package com.bestcem.xm.txtanls.grpc.service;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.uitls.GRpcUtil;
import com.bestcem.xm.txtanls.grpc.convert.TopicConvert;
import com.bestcem.xm.txtanls.grpc.service.param.keyword.ListKeywordParam;
import com.bestcem.xm.txtanls.grpc.service.param.keyword.UpdateKeywordParam;
import com.bestcem.xm.txtanls.grpc.v1.services.*;
import com.bestcem.xm.txtanls.service.KeywordService;
import com.bestcem.xm.txtanls.service.dto.keyword.KeywordCountDTO;
import com.bestcem.xm.txtanls.service.dto.keyword.ListKeywordDTO;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author liheng
 * @version 1.0
 * @date 2021/5/10 17:18
 */
@Slf4j
@GrpcService
public class KeywordGrpcService extends KeywordServiceGrpc.KeywordServiceImplBase {

    @Resource
    private KeywordService keyWordService;

    /**
     * 话题相关，关键词DTO转为grpc传输类
     *
     * @param keywordCountDTOS 关键词DTO list
     * @return proto 关键词message
     * @author liheng
     * @date 2021/5/13 21:13
     */
    public static List<Keyword> convertKeyWordDTOList(List<KeywordCountDTO> keywordCountDTOS) {

        ArrayList<Keyword> keywords = new ArrayList<>(keywordCountDTOS.size());
        for (KeywordCountDTO keywordCountDTO : keywordCountDTOS) {
            Keyword.Builder builder = Keyword.newBuilder();
            if (!ObjectUtils.isEmpty(keywordCountDTO.getCount())) {
                builder.setCount(keywordCountDTO.getCount());
            }
            if (StringUtils.hasText(keywordCountDTO.getName())) {
                builder.setName(keywordCountDTO.getName());
            }
            List<Topic> topics = TopicConvert.convertTopicDTOList(keywordCountDTO.getTopics());
            if (CollUtil.isNotEmpty(topics)) {
                builder.addAllTopics(topics);
            }
            builder.setRadio(keywordCountDTO.getRadio());
            keywords.add(builder.build());
        }
        return keywords;
    }

    /**
     * grpc服务实现方法-关键词提及率查询
     *
     * @param request
     * @param responseObserver
     * @author liheng
     * @date 2021/5/13 22:03
     */
    @Override
    public void listKeyword(ListKeywordRequest request, StreamObserver<ListKeywordResponse> responseObserver) {
        ListKeywordParam listKeywordParam = new ListKeywordParam(request.getProjectId(), request.getStatusValue(), request.getFieldId(), request.getOrgId());

        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(listKeywordParam);
        ListKeywordResponse.Builder builder = ListKeywordResponse.newBuilder();
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        ServiceResult<ListKeywordDTO> listServiceResult = keyWordService.listKeyword(listKeywordParam);
        if (listServiceResult.isSuccess()) {
            ListKeywordDTO data = listServiceResult.getData();
            builder.addAllKeywords(convertKeyWordDTOList(data.getKeywords()));
            builder.setTotal(data.getTotal());
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error(listServiceResult.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
        return;
    }

    /**
     * grpc服务实现方法-屏蔽关键词新增和删除
     *
     * @param request
     * @param responseObserver
     * @author liheng
     * @date 2021/5/13 22:03
     */
    @Override
    public void updateKeyword(UpdateKeywordRequest request, StreamObserver<UpdateKeywordResponse> responseObserver) {
        UpdateKeywordParam updateKeywordParam = new UpdateKeywordParam(request.getProjectId(), request.getKeyword(), request.getStatusValue());

        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(updateKeywordParam);
        UpdateKeywordResponse.Builder builder = UpdateKeywordResponse.newBuilder();
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        ServiceResult<Object> serviceResult = keyWordService.updateKeyword(updateKeywordParam);
        if (serviceResult.isSuccess()) {
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
        return;
    }
}
